package com.tianxiao.faas.application.web.corner;

import com.google.common.collect.Lists;
import com.tianxiao.faas.api.param.corner.BannerSaveParam;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import com.tianxiao.faas.common.util.DateUtils;
import com.tianxiao.faas.common.util.PageUtils;
import com.tianxiao.faas.mapper.dao.BannerModelMapper;
import com.tianxiao.faas.mapper.model.BannerModel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/banner/")
public class BannerController {
    @Resource
    private BannerModelMapper bannerModelMapper;

    @RequestMapping(value = "/list")
    public Result<List<BannerSaveParam>> list() {

        long currentTimeMillis = System.currentTimeMillis();
        List<BannerModel> list = bannerModelMapper.list(currentTimeMillis);
        if (CollectionUtils.isEmpty(list)) {
            return Result.builder().data(Lists.newArrayList()).success(true);
        }
        List<BannerSaveParam> result = Lists.newArrayList();
        BannerSaveParam e = null;
        for (BannerModel param : list) {
            e = new BannerSaveParam();
            e.setBeginTime(DateUtils.format(param.getStartTime()));
            e.setEndTime(DateUtils.format(param.getEndTime()));
            e.setId(param.getId());
            e.setName(param.getName());
            e.setUrl(param.getImg());
            e.setObjectId(param.getLink());
            e.setType(param.getType());
            result.add(e);
        }
        return Result.builder().data(result).success(true);
    }
}
